package org.zhouhe.tag.article;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zhouhe.modules.article.service.ArticleService;
import org.zhouhe.util.Utils;
import org.zhouhe.vo.Article;
import org.zhouhe.vo.ArticleType;

public class ArticleWithLogo extends TagSupport {

	private static final long serialVersionUID = -5129179921556811260L;
	private ServletContext servletContext;
	private WebApplicationContext wac;
	private ArticleService articleService;

	private Integer typeId;
	private Integer subTypeCount = 10;
	private Integer resultSize = 10;
	private String className;

	public int doStartTag() {
		try {
			String menuClass = Utils.generateGUID();
			String contentClass = Utils.generateGUID();
			StringBuilder html = new StringBuilder();
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			servletContext = request.getSession().getServletContext();
			wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
			articleService = (ArticleService) wac.getBean("articleService");
			ArticleType articleType = articleService.getArticleType(typeId);
			Set<ArticleType> articleTypes = articleType.getArticleTypes();
			html.append("<div class='"+ className +"'>");
			html.append("  <ul class='blockTag-title-ul'>");
			html.append("    <li class='blockTag-title-text'>"+ articleType.getName() +"</li>");
			List<ArticleType> articleTypeList = new ArrayList<ArticleType>();
			Iterator<ArticleType> it = articleTypes.iterator();
			int count = 0;
			String menuClassName = "block-title-hover";
			Map<Integer, List<Article>> map = new LinkedHashMap<Integer, List<Article>>();
			while(it.hasNext() && count < subTypeCount){
				ArticleType subType = it.next();
				Integer tid = subType.getArticleTypeId();
				List<Article> subArticleLst = articleService.getArticleByType(tid, null, 8);
				map.put(tid, subArticleLst);
				String menuID = Utils.generateGUID();
				String contentID = Utils.generateGUID();
				subType.setIdPath(contentID);
				html.append("<li class='"+ menuClass +" "+ menuID +" "+ menuClassName +"' onMouseOver=\"mouseOverType('"+ menuClass +"', '"+ menuID +"','block-title-normal','block-title-hover', '"+ contentClass +"', '"+ contentID +"')\"><a href='articleList4txix.action?articleType.articleTypeId="+ subType.getArticleTypeId() +"'>"+ subType.getName() +"</a></li>");
				articleTypeList.add(subType);
				count++;
				menuClassName = "block-title-normal";
			}
			html.append("    <li style='float:right'><a target='_blank' href='articleBlocks4t1ix.action?articleType.articleTypeId="+ typeId +"'>更多&gt;&gt;</a></li>");
			html.append("  </ul>");
			html.append("");
			html.append("  <div class='blockTag-content-div'>");
			String divDisplay = "block";
			for(ArticleType type : articleTypeList){
				
				html.append("    <div style='display: "+ divDisplay +";' class='"+ contentClass +" "+ type.getIdPath() +"'>");
				divDisplay = "none";
				html.append("      <IMG class='report-type-logo' src='upload/images/type_logo/"+ type.getLogo() +"'/>");
				html.append("      <ul class='blockLogo-content-list-ul'>");
				Iterator<Article> articles = map.get(type.getArticleTypeId()).iterator();
				if (articles != null) {
					while(articles.hasNext()){
						Article art = articles.next();
						html.append("      <li>• <a href='article.action?article.articleId="+ art.getArticleId() +"&articleType.articleTypeId="+ type.getArticleTypeId() +"'>" + art.getTitle() + "</a></li>");
					}
				}
				html.append("      </ul>");
				html.append("    </div>");
			}
			html.append("  </div>");
			html.append("  <div style='clear:both;'></div>");
			html.append("</div>");
			html.append("");
			html.append("");
			html.append("");
			JspWriter out = pageContext.getOut();
			out.print(html.toString());
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	public void setTypeId(Integer typeId) {
		this.typeId = typeId;
	}

	public Integer getTypeId() {
		return typeId;
	}

	public void setSubTypeCount(Integer subTypeCount) {
		this.subTypeCount = subTypeCount;
	}

	public Integer getSubTypeCount() {
		return subTypeCount;
	}

	public void setResultSize(Integer resultSize) {
		this.resultSize = resultSize;
	}

	public Integer getResultSize() {
		return resultSize;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getClassName() {
		return className;
	}

}
